package br.edu.sistemaacademico.builder;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import br.edu.sistemaacademico.builder.impl.DeleteSQLBuilder;
import br.edu.sistemaacademico.builder.impl.InsertSQLBuilder;
import br.edu.sistemaacademico.builder.impl.SelectSQLBuilder;
import br.edu.sistemaacademico.builder.impl.UpdateSQLBuilder;
import br.edu.sistemaacademico.dao.DaoGenerico;
import br.edu.sistemaacademico.pojo.Classe;
import br.edu.sistemaacademico.pojo.Disciplina;
import br.edu.sistemaacademico.pojo.Professor;
import br.edu.sistemaacademico.pojo.Turma;

public class TurmaDAO extends DaoGenerico{

	public static final List<String> COLUNAS = Arrays.asList(new String[] {
			"codigo", "tamanho", "periodo", "professor", "classe", "status" });
	
	public List<Object> getCampos(){
		return (List) COLUNAS;
	}
	@Override
	public void alterar(Object o) throws SQLException {
		Turma d = (Turma) o;
		List<Object> valores = Arrays.asList(new Object[] { d.getId(),
				d.getTamanho(), d.getPeriodo(), d.getProfessor(), d.getClasse(), d.getStatus() });

		String condicao = "codigo = " + d.getId() + " and status=1";
		SQLDirector dir = new SQLDirector();
		UpdateSQLBuilder update = new UpdateSQLBuilder();

		update.setNomeTabela("tb_TURMA");
		update.setColunas(COLUNAS);
		update.setValores(valores);
		update.setCondicao(condicao);

		dir.buildSQL(update);
		try {
			executeCommand(update.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}		
	}

	public List<Object> getAll( String condicao, String tabelasAdicionais, String ordenacao){
		Turma turma = new Turma();
		List<Turma> list = new LinkedList<Turma>();
		SQLDirector dir = new SQLDirector();
		SelectSQLBuilder select = new SelectSQLBuilder();

		select.setCondicao(condicao+" and status =1");
		select.setNomeTabela("tb_Turma a"+tabelasAdicionais);
		select.setColunas(COLUNAS);
		// select.setValores(valores);
		select.colunasToReturn = "a.*";
		select.orderBy = ordenacao;
		
		dir.buildSQL(select);
		System.out.println(select.getSQL());
		try {
			ResultSet rs = executeQuery(select.getSQL());
			while (rs.next())
				list.add((Turma) informacoes(rs));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return (List) list;
	}

	@Override
	public Object getObject(String condicao, String tabelasAdicionais) throws SQLException {
		Turma turma = new Turma();
		SQLDirector dir = new SQLDirector();
		SelectSQLBuilder select = new SelectSQLBuilder();

		select.setNomeTabela("tb_Turma a"+tabelasAdicionais);
		select.setColunas(COLUNAS);
		// select.setValores(valores);
		select.setCondicao(condicao+" and status=1");
		select.colunasToReturn = "a.*";

		dir.buildSQL(select);
		System.out.println(select.getSQL());
		try {
			ResultSet rs = executeQuery(select.getSQL());
			if (rs.next())
				turma = (Turma) informacoes(rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return turma;
	}

	@Override
	public void gravar(Object o) throws SQLException {
		Turma d = (Turma) o;
		List<Object> valores = Arrays.asList(new Object[] { d.getId(),
				d.getTamanho(), d.getPeriodo(), d.getProfessor().getId(), d.getClasse().getId(),1 });
		SQLDirector dir = new SQLDirector();
		InsertSQLBuilder insert = new InsertSQLBuilder();

		insert.setNomeTabela("tb_turma");
		insert.setColunas(COLUNAS);
		insert.setValores(valores);

		dir.buildSQL(insert);
		try {
			executeCommand(insert.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}

	@Override
	public Object informacoes(ResultSet rs) throws SQLException {
		Turma t = new Turma();
		ProfessorDAO professorDao = new ProfessorDAO();
		ClasseDAO classeDao =  new ClasseDAO();
		t.setProfessor((Professor)professorDao.getObject("codigo ="+rs.getLong(COLUNAS.get(3)), ""));
		t.setClasse((Classe)classeDao.getObject(" codigo="+rs.getLong(COLUNAS.get(4)), ""));
		t.setId(rs.getLong(COLUNAS.get(0)));
		t.setTamanho(rs.getInt(COLUNAS.get(1)));
		t.setPeriodo(rs.getInt(COLUNAS.get(2)));
		//t.getProfessor().setId(rs.getLong(COLUNAS.get(3)));
		//t.getClasse().setId(rs.getLong(COLUNAS.get(4)));
		//System.out.println("Classe: "+t.getClasse().getId()+" turma="+t.getId());
		return t;
	}

	@Override
	public void remover(Object o) throws SQLException {
		Turma d = (Turma) o;
		List<Object> valores = Arrays.asList(new Object[] { d.getId(),
				d.getTamanho(), d.getPeriodo(), d.getProfessor(), d.getClasse(), d.getStatus() });
		SQLDirector dir = new SQLDirector();
		DeleteSQLBuilder delete = new DeleteSQLBuilder();

		delete.setNomeTabela("tb_turma");
		delete.setColunas(COLUNAS);
		delete.setValores(valores);
		dir.buildSQL(delete);
		try {
			executeCommand(delete.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public void deletar(Object o) throws SQLException {
		Turma d = (Turma) o;
		List<Object> valores = Arrays.asList(new Object[] { d.getId(),
				d.getTamanho(), d.getPeriodo(), d.getProfessor(), d.getClasse(), 0 });

		String condicao = "codigo = " + d.getId() + " and status=1";
		SQLDirector dir = new SQLDirector();
		UpdateSQLBuilder update = new UpdateSQLBuilder();

		update.setNomeTabela("tb_TURMA");
		update.setColunas(COLUNAS);
		update.setValores(valores);
		update.setCondicao(condicao);

		dir.buildSQL(update);
		try {
			executeCommand(update.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}		
	}
}
